<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/easyui_insdep_head :: easyui_insdep_head" th:with="title='重入货'">
</head>
<link th:href="@{/filter/Fliter.css}" rel="stylesheet"/>
<script type="text/javascript" th:src="@{/filter/Filter.js}"/>
<body>

<input type="hidden" id="queryid" th:value="${param.shipmentRecordId == null ? '' : param.shipmentRecordId[0]}"/>

<table id="dg" class="easyui-datagrid" style="width:100%;height:100%"
       data-options="
				singleSelect: true,
                toolbar: '#tb',
                onClickRow: onClickRow,
                rownumbers: true,
			">
    <script type="text/javascript">
        //<![CDATA[
        $(function () {
            if (!$("#queryid").val()) {
                alert("未传值");
                return;
            }

            $('#dg').datagrid({
                url: "/shipment/shipmentItem",
                method: 'get',
                queryParams: {
                    shipmentRecordId: $("#queryid").val()
                },
                columns: [[
                    {field: 'wareId', hidden: true},
                    {
                        field: 'wareName', title: '商品名称', width: '40%',
                        editor: {
                            type: 'combobox',
                            options: {
                                valueField: 'wareName',
                                textField: 'wareName',
                                method: 'get',
                                hasDownArrow: true,
                                url: '/ware/listByNameAndBarCode',
                                mode: 'remote',
                                required: true,
                                onBeforeLoad: function (param) {
                                    if (param == null || param.q == null || param.q == '') {
                                        var value = $('#dg').datagrid('getRows')[editIndex]['wareName'];
                                        if (value && value != '') {
                                            param.q = value;
                                            return true;
                                        }
                                        return false;
                                    }
                                },
                                onSelect: function (record) {
                                    $('#dg').datagrid('getRows')[editIndex]['wareId'] = record.wareId;
                                    updateStocknum($('#dg').datagrid('getRows')[editIndex]);
                                },
                                formatter: function (row) {
                                    return '<span class="item-text">(条码:' + row.wareBarCode + ')' + row.wareName + '</span>';
                                },
                                icons: [{
                                    iconCls: 'icon-search',
                                    handler: function (e) {
                                        $("#ajaxWin").window('open');
                                    }
                                }]
                            }
                        }
                    }, {
                        field: 'repertoryId', hidden: true
                    }, {
                        field: 'repertoryName',
                        title: "出货仓库",
                        width: "8%",
                        align: 'right',
                        editor: {
                            type: 'combobox',
                            options: {
                                valueField: 'repertoryName',
                                textField: 'repertoryName',
                                method: 'get',
                                hasDownArrow: true,
                                url: '/repertory',
                                editable: false,
                                required: true,
                                onSelect: function (record) {
                                    $('#dg').datagrid('getRows')[editIndex]['repertoryId'] = record.repertoryId;
                                    updateStocknum($('#dg').datagrid('getRows')[editIndex]);
                                }
                            }
                        }
                    }, {
                        field: 'shiptTgtId', hidden: true
                    }, {
                        field: 'shiptTgtName',
                        title: "出货渠道",
                        width: "8%",
                        align: 'right',
                        editor: {
                            type: 'combobox',
                            options: {
                                valueField: 'shiptTgtName',
                                textField: 'shiptTgtName',
                                method: 'get',
                                hasDownArrow: true,
                                url: '/shipmentTarget',
                                editable: false,
                                required: true,
                                onSelect: function (record) {
                                    $('#dg').datagrid('getRows')[editIndex]['shiptTgtId'] = record.shiptTgtId;
                                }
                            }
                        }
                    },
                    {
                        field: 'shiptItemNum', title: '出货数量', width: '10%', align: 'right',
                        editor: {
                            type: 'numberbox',
                            options: {
                                required: true
                            }
                        }
                    },
                    {
                        field: 'shiptItemPx', title: '出货价格', width: '10%', align: 'right',
                        formatter: function (value, row, index) {
                            value = value + "";

                            if (!value) {
                                return;
                            }

                            var endIndex = value.length;

                            while (value.charAt(endIndex - 1) == 0) {
                                endIndex--;
                            }

                            if (value.charAt(endIndex - 1) == '.') {
                                endIndex--;
                            }
                            return value.substring(0, endIndex);
                        },
                        editor: {
                            type: 'numberbox', options: {
                                required: true,
                                prefix: "￥",
                                precision: "8",
                                formatter: function (value) {
                                    if (!value) {
                                        return;
                                    }

                                    var endIndex = value.length;

                                    while (value.charAt(endIndex - 1) == 0) {
                                        endIndex--;
                                    }

                                    if (value.charAt(endIndex - 1) == '.') {
                                        endIndex--;
                                    }
                                    return value.substring(0, endIndex);
                                }
                            }
                        }
                    },{
                        field: 'stockNum', title: '库存数量', width: '10%',
                    }
                ]]
            });
        });
        //]]>

        function updateStocknum (row) {
            $.get("/stock/stockNum", {
                wareId: row.wareId,
                repertoryId : row.repertoryId
            }, function (data) {
                var i=$('#dg').datagrid('getRowIndex',row);
                var td=$('.datagrid-body td[field="stockNum"]')[i];
                var div = $(td).find('div')[0];
                $(div).text(data);
            });
        }
    </script>
</table>

<div id="ajaxWin" class="easyui-window" title="商品选择工具"
     data-options="modal:true,collapsible:true,closed:true,maximizable:false,minimizable:false,resizable:false"
     style="width:100%;height:100%;overflow: hidden">
    <table id="tt" class="easyui-datagrid" style="width:100%;height:100%" method="get"
           toolbar="#ttb" pageList='[1,10,20,30,50]' singleSelect="true"
           rownumbers="true" pagination="true">
        <thead>
        <tr>
            <th field="wareName" width="500">名称</th>
            <th field="wareDes" width="160">描述</th>
            <th field="wareBarCode" width="160">条形码</th>
        </tr>
        </thead>
        <div id="ttb" style="padding:3px">
            <div id="filter"></div>
            <span>名称:</span>
            <input id="wareName" style="line-height:26px;border:1px solid #ccc"/>
            <span>描述:</span>
            <input id="wareDes" style="line-height:26px;border:1px solid #ccc"/>
            <span>条形码:</span>
            <input id="wareBarCode" style="line-height:26px;border:1px solid #ccc"/>
            <input id="componentId" type="hidden"/>
            <input id="componentParameterDetailId" type="hidden"/>
            <input type="checkbox" id="isFuzzy"/>模糊查询
            <a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">查找</a>
            <a href="#" class="easyui-linkbutton" plain="true" onclick="doReSet()">重置</a>

            <script type="text/javascript">
                //<![CDATA[
                $(function () {
                    //单选
                    $('#filter').comboboxfilter({
                        url: '/component/filter',
                        method: 'get',
                        text: "配件",
                        onSelect: function (itemData) {
                            $("#componentid").val(itemData.id);
                            doSearch();
                            // 获取他的属性
                            $.get("/componentParameter", {
                                cmpId: itemData.id
                            }, function (data) {
                                if (typeof (data) == typeof ("string")) {
                                    data = $.parseJSON(data);
                                }
                                // 遍历属性
                                $.each(data, function (i, item) {
                                    // 新建一个div
                                    var id = "filter" + i;
                                    $('#filter').after("<div id = '" + id + "' name='Dfilter'/ >")
                                    // 为该div初始化值
                                    id = '#' + id;
                                    $(id).comboboxfilter({
                                        url: '/componentParameterDetail/filter',
                                        method: 'get',
                                        text: item.compParamName,
                                        param: {
                                            compParamId: item.compParamId
                                        },
                                        onSelect: function (itemData) {
                                            $("#componentParameterDetailId").val(itemData.id);
                                            doSearch();
                                        },
                                        unSelect: function (itemData) {
                                            $("#componentParameterDetailId").val("");
                                            doSearch();
                                        }
                                    });
                                });
                            })
                        },
                        unSelect: function (itemData) {
                            $("#componentId").val("");
                            doSearch();
                            $("div[name='Dfilter']").each(function () {
                                $(this).comboboxfilter('clear');
                                $(this).remove();
                            });
                        }
                    });

                    $('#tt').datagrid({
                        url: "/wareParameterDetail/parameterDetailWare/list",
                        onSelect: function (index, row) {
                            $('#dg').datagrid('getRows')[editIndex]['wareid'] = row.wareid;
                            updateStocknum($('#dg').datagrid('getRows')[editIndex]);
                            var ed = $('#dg').datagrid('getEditor', {index: editIndex, field: 'wareName'});
                            $(ed.target).combobox('setText', row.warename);
                            $(ed.target).combobox('setValue', row.warename);
                            $("#ajaxWin").window("close");
                        }
                    });
                });

                function doSearch() {
                    $('#tt').datagrid('load', {
                        wareName: $('#wareName').val(),
                        wareDes: $('#wareDes').val(),
                        wareBarCode: $('#wareBarCode').val(),
                        isFuzzy: $('#isFuzzy').is(':checked'),
                        componentId: $('#componentid').val(),
                        componentParameterDetailId: $('#componentParameterDetailId').val(),
                    });
                }
                function doReSet() {
                    $('#wareName').val("");
                    $('#wareDes').val("");
                    $('#wareBarCode').val();
                    doSearch();
                }
                //]]>
            </script>
        </div>
    </table>
</div>


<div id="tb">
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"
       onclick="append()">增加出货商品</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true"
       onclick="removeit()">移除出货商品</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true"
       onclick="accept()">编辑完成</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true"
       onclick="reject()">撤销一次编辑</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true"
       onclick="getChanges()">提交出货商品清单</a>
</div>

<script type="text/javascript">
    //<![CDATA[
    var editIndex = undefined;
    function endEditing() {
        if (editIndex == undefined) {
            return true
        }
        if ($('#dg').datagrid('validateRow', editIndex)) {
            $('#dg').datagrid('endEdit', editIndex);

            editIndex = undefined;

            return true;
        } else {
            return false;
        }
    }
    function onClickRow(index) {
        if (editIndex != index) {
            if (endEditing()) {
                editIndex = index;
                updateStocknum($('#dg').datagrid('getRows')[editIndex]);
                $('#dg').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
            } else {
                $('#dg').datagrid('selectRow', editIndex);
            }
        }
    }
    function append() {
        if (endEditing()) {
            $('#dg').datagrid('appendRow', {});
            editIndex = $('#dg').datagrid('getRows').length - 1;
            $('#dg').datagrid('selectRow', editIndex)
                .datagrid('beginEdit', editIndex);
        }
    }
    function removeit() {
        if (editIndex == undefined) {
            return
        }
        $('#dg').datagrid('cancelEdit', editIndex)
            .datagrid('deleteRow', editIndex);
        editIndex = undefined;
    }
    function accept() {
        if (endEditing()) {
            $('#dg').datagrid('acceptChanges');

        }
    }
    function reject() {
        $('#dg').datagrid('rejectChanges');
        editIndex = undefined;
    }
    function getChanges() {
        accept();

        var jsonstr = "[]";
        var jsonarray = eval('(' + jsonstr + ')');

        for (var i = 0; i < $('#dg').datagrid('getRows').length; i++) {
            $('#dg').datagrid('selectRow', i);// 关键在这里
            var arr =
                {
                    "wareId": $('#dg').datagrid('getSelected').wareId,
                    "repertoryId": $('#dg').datagrid('getSelected').repertoryId,
                    "shiptTgtId": $('#dg').datagrid('getSelected').shiptTgtId,
                    "shiptRecId" : $("#queryid").val(),
                    "shiptItemNum": $('#dg').datagrid('getSelected').shiptItemNum,
                    "shiptItemPx": $('#dg').datagrid('getSelected').shiptItemPx,
                }
            jsonarray.push(arr);
        }

        $.ajax({
            url: "/shipment/shipmentRecord",
            type: "PUT",
            datType: "JSON",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(jsonarray),
            success: function (data) {
                var data = data == "success" ? "重出货成功" : "重出货失败";
                $.messager.alert('结果', data, 'info');
                $("#dg").datagrid("reload");
            }
        });
    }
    //]]>
</script>


</body>
</html>